package com.yuefresh.core;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * 
 * 
 * @author xufeng
 * @since 2010-12-28
 * 
 * @param <T>
 * @param <PK>
 */
public abstract class EntityManager<T extends Model, PK extends Serializable> extends EntityAtomManager<T, PK> {
	public EntityManager() {}
	
	public void save(final T entity) {
		Date date = new Date();
		if (entity.getCreateTime() == null) entity.setCreateTime(date);
		entity.setUpdateTime(date);
		hibernateInvoker.save(entity);
	}
	
	public void save(List<T> list) {
		for(T entity: list){
			save(entity);
		}
	}
	/**
	 * 通过修改 isValid 有效性标识，来删除数据，而不是直接从数据库删除
	 * 
	 * @param entity
	 * @return delete object
	 */
	public T delete(final T entity) {
		entity.setUpdateTime(new Date());
		super.delete(entity);
		return entity;
	}
	
	public void delete(List<T> list){
		for(T entity: list){
			entity.setUpdateTime(new Date());
		}
		super.delete(list);
	}
	
}
